package com.hongkzh.www.other.api;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * 接口参数转换工具类
 */
public class UrlWrapper {
    private static UrlWrapper urlWrapper = new UrlWrapper();
    private static String baseURL = "";
    private static Map<String, String> params = new HashMap<>();

    private UrlWrapper() {

    }

    public UrlWrapper(String url) {
        this.baseURL = url;
    }

    public static UrlWrapper getInstance() {
        params.clear();
        return urlWrapper;
    }

    public UrlWrapper addParameter(String name, String value) {
        params.put(name, value);
        return urlWrapper;
    }

    public String getRequestURL() {
        if (params.isEmpty()) {
            return baseURL;
        } else {
            String result = baseURL + "?";
            Iterator<String> paramIt = params.keySet().iterator();
            while (paramIt.hasNext()) {
                String key = paramIt.next();
                if (params.get(key) != null) {
                    String value = params.get(key).toString();
                    result += key + "=" + value + "&";
                }
            }
            return result.substring(0, result.length() - 1);
        }
    }

    public String getRequestURL2() {
        if (params.isEmpty()) {
            return this.baseURL;
        } else {
            String result = this.baseURL + "&";
            Iterator<String> paramIt = params.keySet().iterator();
            while (paramIt.hasNext()) {
                String key = paramIt.next();
                if (params.get(key) != null) {
                    String value = params.get(key).toString();
                    result += key + "=" + value + "&";
                }
            }
            return result.substring(0, result.length() - 1);
        }
    }

    public String getRequestURL(String url, Map<String, String> paramss) {
        if (paramss.isEmpty()) {
            return url;
        } else {
            String result = url + "?";
            Iterator<String> paramIt = paramss.keySet().iterator();
            while (paramIt.hasNext()) {
                String key = paramIt.next();
                if (paramss.get(key) != null) {
                    String value = paramss.get(key).toString();
                    result += key + "=" + value + "&";
                }
            }
            return result.substring(0, result.length() - 1);
        }
    }

    public static String getRequestURL(Map<String, String> paramss) {
        if (paramss.isEmpty()) {
            return baseURL;
        } else {
            String result = baseURL + "?";
            Iterator<String> paramIt = paramss.keySet().iterator();
            while (paramIt.hasNext()) {
                String key = paramIt.next();
                if (paramss.get(key) != null) {
                    String value = paramss.get(key).toString();
                    result += key + "=" + value + "&";
                }
            }
            return result.substring(0, result.length() - 1);
        }
    }
}
